package dao.admin.impl;

import dao.BaseDao;
import dao.admin.CardAddDao;
import entity.Borrow_card;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


/**
 * CardAddDaoImpl
 *
 * @author lenovo
 * @since 2023/9/4
 */
public class CardAddDaoImpl extends BaseDao implements CardAddDao {
    @Override
    public int add(Borrow_card borrow_card) {
        String sql = "insert into borrow_card(password, reader, rule_id, status) values(?,?,?,?)";
        return executeUpdate(sql,borrow_card.getPassword(),borrow_card.getReader(),borrow_card.getRule_id(),borrow_card.getStatus());
    }

    @Override
    public int getId(Borrow_card borrow_card) {
        String sql = "select id from borrow_card where password=? and reader=? and rule_id=? and status=? limit 1";
        Connection conn = getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        int ID = 0;
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,borrow_card.getPassword());
            pstmt.setString(2,borrow_card.getReader());
            pstmt.setInt(3,borrow_card.getRule_id());
            pstmt.setInt(4,borrow_card.getStatus());
            rs = pstmt.executeQuery();
            if (rs.next()){
                ID = rs.getInt("id");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(rs,pstmt,conn);
        }
        return ID;
    }
}
